package com.itljm.cxrMarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itljm.cxrMarket.entity.SysPermission;
import com.itljm.cxrMarket.entity.SysRolePermission;
import com.itljm.cxrMarket.entity.dto.RolePermissionDTO;
import com.itljm.cxrMarket.mapper.SysPermissionMapper;
import com.itljm.cxrMarket.service.SysRolePermissionService;
import com.itljm.cxrMarket.mapper.SysRolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService{
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<SysPermission> selectAllPermission() {
        return sysPermissionMapper.selectAllPermission();
    }

    @Override
    public void assignPermissionsToRole(Long roleId, List<Long> permissionIds) {
        // 清除现有权限关联
        sysRolePermissionMapper.deleteByRoleId(roleId);

        // 插入新的权限关联
        for (Long permissionId : permissionIds) {
            sysRolePermissionMapper.insertRolePermission(roleId, permissionId);
        }
    }

    @Override
    public List<String> getRolePermissions(Long roleId) {
        return sysRolePermissionMapper.selectPermissionNamesByRoleId(roleId);
    }

    @Override
    public List<RolePermissionDTO> selectPermissionByRoleId(Long roleId) {
        return sysRolePermissionMapper.selectPermissionByRoleId(roleId);
    }
}




